//
//  shopMessageVm.swift
//  Pods
//
//  Created by eme on 2017/3/28.
//
//

import SwiftyJSON
import RxSwift
import Result
import ModelProtocol
import NetWorkCore

class shopMessageVm{
    
    let backMessage = PublishSubject<ShopMseeageModel>()
    let getShopMessage = PublishSubject<String>()
    
    let error = PublishSubject<MikerError>()
    
    let loading : Observable<Bool>
    
    let disposeBag = DisposeBag()
    
    
    let submitFeedBack = PublishSubject<String>()
    let submitBackDate = PublishSubject<Bool>()
    
    init(){
        
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        let request = getShopMessage.map{ spid in
            return MineApi.getshopMessage(spid: spid)
            }.shareReplay(1)
        
        let response = request.emeRequestApiForObj(ShopMseeageModel.self,activityIndicator:requestIndicator).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] (result) in
            switch result{
            case.success(let data):
                self.backMessage.onNext(data)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        /*
         * 提交意见反馈
         */
        let feedBackRequest = submitFeedBack.map{jsonStr in
            return MineApi.submitFeedBack(jsonStr: jsonStr)
            }.shareReplay(1)
        let feedBackResponse = feedBackRequest.emeRequestApiForRegJson().shareReplay(1)
        
        feedBackResponse.subscribe(onNext:{ Result in
            switch Result{
            case .success(_):
                self.submitBackDate.onNext(true)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        
    }
    
    
}
